package com.shoe.controller;


import com.jptangchina.jxcel.JxcelParser;
import com.shoe.pojo.ShoeDepot;
import com.shoe.pojo.ShoeDepotExample;
import com.shoe.service.ShoeDepotService;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@Controller
@RequestMapping("depot")
public class DepotController {
    @Autowired
    ShoeDepotService shoeDepotService;
    @Autowired
    ResponseResult responseResult;

    @GetMapping("depotall")
    @ResponseBody
    public ResponseResult selectAll(@RequestParam int currPage, @RequestParam int pageSize) {
        responseResult = shoeDepotService.selectAll(currPage, pageSize);
        return responseResult;
    }

    @GetMapping("depotByTime")
    @ResponseBody
    public ResponseResult selectByTime(int currPage,int pageSize,String conTime) throws ParseException {
        ShoeDepotExample shoeDepotExample = new ShoeDepotExample();
        ShoeDepotExample.Criteria criteria = shoeDepotExample.createCriteria();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date before = simpleDateFormat.parse(conTime);
        criteria.andUpdateTimeBetween(before,new Date());
        responseResult = shoeDepotService.selectByTime(currPage , pageSize , shoeDepotExample);
        return responseResult;
    }


    @GetMapping("delOneDep")
    @ResponseBody
    public int delById(Long depotId) {
        return shoeDepotService.delById(depotId);
    }

    @ResponseBody
    @RequestMapping("delDepots")
    public ResponseResult delMany(String ids) {
        String[] arr = ids.split(",");
        int count = 0;
        for (String i : arr) {
            count += shoeDepotService.delById(Long.valueOf(i));
        }
        if (count == arr.length) {
            responseResult.setCode(1);
            responseResult.setMsg("库存删除成功");
            responseResult.setData(count);
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("库存删除失败");
            responseResult.setData(count);
        }
        return responseResult;
    }

    @Transactional
    @ResponseBody
    @RequestMapping("updDepots")
    public ResponseResult updateOne(String info) {
        Date date = new Date();
        String[] arr = info.split(",");
        ShoeDepot shoeDepot = new ShoeDepot();
        shoeDepot.setDepotId(Long.valueOf(arr[0]));
        shoeDepot.setDepotGoodCount(Short.valueOf(arr[1]));
        shoeDepot.setUpdateTime(date);
        System.out.println(date);

        int count = shoeDepotService.updateByPrimaryKeySelective(shoeDepot);

        if (count == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("库存修改成功");
            responseResult.setData(count);
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("库存修改失败");
            responseResult.setData(count);
        }
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("import")
    public ResponseResult exImport(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String targetFilePath = "D:\\";
        File myfile = new File(targetFilePath + File.separator + fileName);
        file.transferTo(myfile);
        List<ShoeDepot> receive = JxcelParser.parser().parseFromFile(ShoeDepot.class, myfile);
        int count = 0;
        for (ShoeDepot i : receive) {
            ShoeDepotExample shoeDepotExample = new ShoeDepotExample();
            ShoeDepotExample.Criteria criteria = shoeDepotExample.createCriteria();
            criteria.andDepotGoodIdEqualTo(i.getDepotGoodId());
            List<ShoeDepot> shoeDepots = shoeDepotService.selectByGoodId(shoeDepotExample);
            if(shoeDepots.size() > 0){
                i.setUpdateTime(new Date());
                count += shoeDepotService.updateByExampleSelective(i , shoeDepotExample);
            }else {
                i.setCreateTime(new Date());
                i.setUpdateTime(new Date());
                int n = shoeDepotService.insertSelective(i);
                count += n;
            }
        }
        if (count == receive.size()) {
            responseResult.setCode(1);
            responseResult.setData(count);
            responseResult.setMsg("导入成功");
        }
        else {
            responseResult.setCode(0);
            responseResult.setData(count);
            responseResult.setMsg("导入失败");
        }
        return responseResult;
    }

    @RequestMapping("selectshoeSize")
    @ResponseBody
    public int selectById(@RequestParam String goodId,@RequestParam String goodSize){
        ShoeDepot shoeDepot = new ShoeDepot();
        shoeDepot.setDepotGoodId(Long.valueOf(goodId));
        shoeDepot.setDepotGoodSize(Integer.valueOf(goodSize));

        int a =  shoeDepotService.selectBySize(shoeDepot);

        return a;
    }




}
